using welog.core.model;
using welog.core.Utilities;
using SearchOption = Microsoft.VisualBasic.FileIO.SearchOption;

namespace welog.core;

public class WelogHandle
{
    public List<IWelogTextPlugin> WelogTextPlugins { get; set; }

    public WelogHandle()
    {
        
    }
    
    /// <summary>
    /// 获取markdown存放的文件路径列表
    /// </summary>
    /// <param name="baseDir"></param>
    /// <returns></returns>
    public LogTreeNode GetMarkDownFileList(string baseDir)
    {
        var logTreeNode = new LogTreeNode();
        var dirInfo = new DirectoryInfo(baseDir);
        logTreeNode.Name = dirInfo.Name;
        logTreeNode.Value = dirInfo.FullName;
        logTreeNode.ChildrenDir.AddRange(this.GetLogTreeNodes(baseDir));
        return logTreeNode;
    }
    
    public List<LogTreeNode> GetLogTreeNodes(string baseDir)
    {
        List<LogTreeNode> logTreeNodes = new List<LogTreeNode>();
        //年
        var fileList = Directory.GetDirectories(baseDir).ToList();
        fileList.Sort((x, y) => Convert.ToInt32(y.Substring(y.IndexOf('\\')+1)) - Convert.ToInt32(x.Substring(x.IndexOf('\\')+1)));
        foreach (var logYearDir in fileList)
        {
            var childrenNode = this.GetLogTreeNodesCore(logYearDir);
            logTreeNodes.Add(childrenNode);
        }

        return logTreeNodes;
    }
    
    private LogTreeNode GetLogTreeNodesCore( string baseDir)
    {
        var logTreeNode = new LogTreeNode();
        var fileList = Directory.GetDirectories(baseDir).ToList();
        fileList.Sort((x, y) =>  Convert.ToInt32(y.Substring(y.LastIndexOf('\\')+1))-Convert.ToInt32(x.Substring(x.LastIndexOf('\\')+1)) );
        
        foreach (var logDir in fileList)
        {
            var childrenDir = this.GetLogTreeNodesCore(logDir);
            logTreeNode.ChildrenDir.Add(childrenDir);
        }

        foreach (var logFile in Directory.GetFiles(baseDir))
        {
            logTreeNode.ChildrenFile.Add(logFile);
        }
        
        DirectoryInfo directoryInfo = new DirectoryInfo(baseDir);
        logTreeNode.Name = directoryInfo.Name;
        logTreeNode.Value = directoryInfo.FullName;
        return logTreeNode;
    }

    /// <summary>
    /// 添加默认文件到指定位置
    /// </summary>
    /// <param name="baseDir"></param>
    /// <returns></returns>
    public bool AddMarkDownFileByCurrentTime(string baseDir,string templeteFilePath)
    {
        baseDir.CreateDir();
        var currentTime = DateTime.Now;
        var childrenDir =Path.Combine(baseDir, $"{currentTime.Year}//{currentTime.Month}//{currentTime.Day}");
        var yesterdayFilePath = this.GetLastTaskNode(baseDir);
        childrenDir.CreateDir();
        var destFileName = Path.Combine(childrenDir, $"{currentTime.Year}-{currentTime.Month}-{currentTime.Day}.md");
        if (!File.Exists(destFileName))
        {
            File.Copy(templeteFilePath,destFileName);
            if (yesterdayFilePath != null)
            {
                this.WelogTextPlugins.ForEach(textplugin =>
                    textplugin.Create(destFileName, yesterdayFilePath.ChildrenFile[0]));
            }
        }
        return true;
    }

    private LogTreeNode GetLastTaskNode(string baseDir)
    {
        var listNode=this.GetLogTreeNodes(baseDir);
        if (listNode.Count > 0)
        {
            return listNode[0].ChildrenDir[0].ChildrenDir[0];
        }

        return null;
    }
}